﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlServerAdmin;
using IQ.BaseWindows;
using IQ.DbBase;

namespace IQ.DbA
{
    public partial class FrmSyncTables : Form
    {
		#region # Constructor # 
        public FrmSyncTables()
        {
            InitializeComponent();
            grdFileTbls.AutoGenerateColumns = false;
            grdServerTbls.AutoGenerateColumns = false;
            grdFields.AutoGenerateColumns = false;
            grdChanges.AutoGenerateColumns = false;
        }
		#endregion
		
		#region - Private variables - 
        private SqlMdTable sqlMD = new SqlMdTable();
        private bool checkExcludes = false;
        #endregion 

		#region - Private methods - 
        private void setMaxProgress(int value)
        {
            pgbAll.Maximum = value;
        }
        private void setProgress(int value)
        {
            pgbAll.Value = value;
        }
        private void setLoadTable(string tableName, bool fileTable)
        {
            if (fileTable)
            {
                lblLoadedTable.BackColor = Color.Red;
                lblLoadedTable.Text = "File table: " + tableName;
            }
            else
            {
                lblLoadedTable.BackColor = Color.Lime;
                lblLoadedTable.Text = "Server table: " + tableName;
            }
        }
        private int findNameInGridNoSort(DataGridView grid, int colIndex, string nameValue)
        {
            int rowInd = -1;
            for (int i = 0; i < grid.RowCount; ++i)
            {
                if (grid[colIndex, i].Value.ToString() == nameValue)
                {
                    rowInd = i;
                    break;
                }
            }
            //
            if (rowInd >= 0)
                grid.CurrentCell = grid.Rows[rowInd].Cells[0];
            else
                grid.CurrentCell = null;
            return rowInd;
        }
        private void compareOne(string tableName)
        {
            checkExcludeList();
            // Look for table in built changes
            int rowInd = findNameInGridNoSort(grdChanges, cfName.Index, tableName);
            // Look for table on server
            int rowIndServer = findNameInGridNoSort(grdServerTbls, csName.Index, tableName);
            // Table not found on server
            rowInd = findNameInGridNoSort(grdFileTbls, cfName.Index, tableName);
            if (rowInd >= 0)
            {
                if (rowIndServer >= 0)
                {
                    sqlMD.CompareTable(tableName);
                    grdFields.DataSource = sqlMD.FieldDiffs;
                    setLoadTable(tableName, false);
                }
                else
                {
                    grdFields.DataSource = sqlMD.FileTableFields(tableName);
                    setLoadTable(tableName, true);
                }
            }
            else
                General.MesDlg("Cannot find file table to compare", 'I');
        }
        private void checkExcludeList()
        {
            if (checkExcludes)
            {
                sqlMD.SaveExludeList(txtExclude.Lines);
                checkExcludes = false;
            }
        }
        #endregion 
		
		//#region - Public properties / variables - 
		//#endregion

		//#region - Public methods - 
		//#endregion
		
		#region - Bind Methods - 
		#endregion
		
		#region - Form Events -
		// This region is for event handler methods that are called without the user knowing about it.
		// Such as the page loading.
        private void FrmSyncTables_Shown(object sender, EventArgs e)
        {
            if (SqlMdFileData.CheckCompareFileExists())
            {
                sqlMD.SetMaxPosition += setMaxProgress;
                sqlMD.SetCurrentPosition += setProgress;
                txtExclude.Lines = sqlMD.Exclusions;
                checkExcludes = false;
                grdFileTbls.DataSource = sqlMD.FileTables;
                grdChanges.DataSource = sqlMD.Changes;
                grdServerTbls.DataSource = sqlMD.ServerTables;
            }
            else
            {
                General.MesDlg("Sql meta data file does not exists, please export meta data and then return.", 'E');
                this.Close();
            }
        }
        private void frmSyncTables_FormClosed(object sender, FormClosedEventArgs e)
        {
            checkExcludeList();
        }
        private void grdFields_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == cffCompare.Index && e.Value != null)
            {
                e.CellStyle.ForeColor = SqlConst.GetCompareColor((byte)e.Value);
                e.Value = SqlConst.GetCompareText((byte)e.Value);
                e.FormattingApplied = true;
            }
        }
        private void txtExclude_TextChanged(object sender, EventArgs e)
        {
            checkExcludes = true;
        }
		#endregion 
		
		#region - Control Methods -
		// This region is for event handler methods that are called knowingly by the user.
		// Such as a button being clicked.
        private void grdChanges_DoubleClick(object sender, EventArgs e)
        {
            if (grdChanges.CurrentRow != null)
            {
                compareOne(grdChanges[ccName.Index, grdChanges.CurrentRow.Index].Value.ToString());
            }
        }

        private void grdServerTbls_DoubleClick(object sender, EventArgs e)
        {
            if (grdServerTbls.CurrentRow != null)
            {
                compareOne(grdServerTbls[csName.Index, grdServerTbls.CurrentRow.Index].Value.ToString());
            }
        }

        private void grdFileTbls_DoubleClick(object sender, EventArgs e)
        {
            if (grdFileTbls.CurrentRow != null)
            {
                compareOne(grdFileTbls[cfName.Index, grdFileTbls.CurrentRow.Index].Value.ToString());
            }
        }
        private void btnCompare_Click(object sender, EventArgs e)
        {
            if (grdFileTbls.CurrentRow != null)
            {
                compareOne(grdFileTbls["cfName", grdFileTbls.CurrentRow.Index].Value.ToString());
            }
            else
                General.MesDlg("No table selected to compare.", 'E');
        }
        private void btnCompareAll_Click(object sender, EventArgs e)
        {
            checkExcludeList();
            sqlMD.CompareAllTables();
        }
        #endregion


    }
}
